package net.xxcl.exam.service;

import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;

import jxl.Cell;
import net.xxcl.exam.mapper.AnswerMapper;
import net.xxcl.exam.mapper.QuestionMapper;
import net.xxcl.exam.mapper.QuestionTypeMapper;
import net.xxcl.exam.pojo.Answer;
import net.xxcl.exam.pojo.Criteria;
import net.xxcl.exam.pojo.Question;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional()
public class QuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private QuestionTypeMapper questionTypeMapper;

    private static final Logger logger = LoggerFactory.getLogger(QuestionService.class);

    @Transactional(readOnly = true)
    public int countByExample(Criteria example) {
        int count = this.questionMapper.countByExample(example);
        logger.debug("count: {}", count);
        return count;
    }
    @Transactional(readOnly = true)
    public Question selectByPrimaryKey(Integer id) {
        return this.questionMapper.selectByPrimaryKey(id);
    }
    @Transactional(readOnly = true)
    public List<Question> selectByExample(Criteria example) {
        return this.questionMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(Integer id) {
        return this.questionMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Question record) {
        return this.questionMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Question record) {
        return this.questionMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(Criteria example) {
        return this.questionMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Question record, Criteria example) {
        return this.questionMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Question record, Criteria example) {
        return this.questionMapper.updateByExample(record, example);
    }

    public int insert(Question record) {
        return this.questionMapper.insert(record);
    }

    public int insertSelective(Question record) {
        return this.questionMapper.insertSelective(record);
    }

    public Boolean saveExcelRow(Cell[] cells,int kemuId){
        try{
            Question question = new Question();
            int typeid = 0;
            String type = cells[0].getContents().trim();
            if(type.contains("单选")) {
                typeid = 1;
            } else if (type.contains("多选")){
                typeid = 2;
            } else if (type.contains("判断")){
                typeid = 3;
            } else if (type.contains("word")){
                typeid = 4;
            } else if (type.contains("excel")){
                typeid = 5;
            } else if (type.contains("ppt")){
                typeid = 6;
            } else if (type.contains("打字")){
                typeid = 7;
            } else {
                throw new RuntimeException("没有这种题型");
            }
            question.setType(typeid);
            question.setKemuid(kemuId);
            question.setZhang(Integer.parseInt(cells[1].getContents().trim()));
            question.setMark(Integer.parseInt(cells[2].getContents().trim()));
            question.setTitle(cells[3].getContents().trim());
            //题型为1,2,3，提取正确答案
            String rightAnswer = "";
            if(typeid==1 || typeid==2 || typeid==3){
                rightAnswer = cells[8].getContents().trim();
            } else if (typeid!=7){
                question.setFilename(cells[9].getContents().trim());
            }
            int questionId = questionMapper.insertSelective(question);
            //题型为1,2,3，保存选项
            if(typeid==1 || typeid==2 || typeid==3){
                for(int i=4;i<8;i++){
                    Answer answer = new Answer();
                    if(cells[i].getContents()==null || cells[i].getContents().trim().equals("")){
                        continue;
                    }
                    answer.setAnswer(cells[i].getContents());
                    answer.setQuestionid(question.getId()); //取出插入的question主键
                    switch (i){
                        case 4:answer.setIsright(rightAnswer.contains("A") || rightAnswer.contains("a"));break;
                        case 5:answer.setIsright(rightAnswer.contains("B") || rightAnswer.contains("b"));break;
                        case 6:answer.setIsright(rightAnswer.contains("C") || rightAnswer.contains("c"));break;
                        case 7:answer.setIsright(rightAnswer.contains("D") || rightAnswer.contains("d"));break;
                    }
                    answerMapper.insert(answer);
                }
            }
            return true;
        } catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
    }
}